﻿namespace AgentJohnson.SmartGenerators
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using Resharper.Abstractions.Generators;
  using Resharper.Abstractions.LiveTemplates;
  using Resharper.Abstractions.Shell;

  /// <summary>Defines the <see cref="SmartGeneratorPopulator"/> class.</summary>
  public class SmartGeneratorPopulator : IGeneratorPopulator
  {
    #region Implemented Interfaces

    #region IGeneratorPopulator

    /// <summary>Populates the specified data context.</summary>
    /// <param name="context">The data context.</param>
    /// <returns>Returns the I enumerable.</returns>
    public IEnumerable<GeneratorBase> Populate(DataContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var scopes = ScopeManager.GetScopes(context).ToList();
      if (!scopes.Any())
      {
        yield break;
      }

      yield return new CreateLiveTemplateGenerator();

      var templates = scopes.SelectMany(LiveTemplateManager.GetLiveTemplates).ToList();

      foreach (var template in templates.OrderBy(t => t.Description))
      {
        yield return new LiveTemplateGenerator(template);
      }
    }

    #endregion

    #endregion
  }
}